<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-02-24
 * Time: 12:15
 */

namespace app\mall\service;

use app\exception\CustomException;
use app\model\Award;
use app\model\Company;
use app\model\Customer;
use app\model\Help;
use app\model\Lottery;
use app\model\Reward;
use think\Db;
use think\Exception;

class LotteryService extends BaseService{

    function lists($where, $pageSize)
    {
        $where['status'] = 1;
        $data = Award::where($where)->field('award_id, cover, type, condition, name, status, open_time')->order('award_id desc')->select();

        $where['status'] = 2;
        $data2 = Award::where($where)->field('award_id, cover, type, condition, name, status, open_time')->order('award_id desc')->paginate($pageSize);
        return [
            'wait'=> $data,
            'open'=> $data2
        ];
    }

    function lists2($where, $pageSize)
    {
        if($where['is_prize'] == 0) unset($where['is_prize']);
        $award_id = Lottery::where($where)->column('award_id');
        $where = [
            'award_id'=> ['in', $award_id]
        ];
        $where['status'] = 1;
        $data = Award::where($where)->field('award_id, cover, type, condition, name, status, open_time')->order('award_id desc')->select();

        $where['status'] = 2;
        $data2 = Award::where($where)->field('award_id, cover, type, condition, name, status, open_time')->order('award_id desc')->paginate($pageSize);
        return [
            'wait'=> $data,
            'open'=> $data2,
            'count'=> sizeof($data)
        ];
    }

    function details($where, $customer_id)
    {
        $data = Award::where($where)->find();
        if($data['status'] == 2)
        {
            $lottery_id = Lottery::where(['award_id'=> $where['award_id'], 'customer_id'=> $customer_id])->column('lottery_id');
            if($lottery_id)
            {
                $coupon = db('my_coupon')->alias('m')
                    ->join('coupon c', 'c.coupon_id = m.coupon_id')
                    ->where(['m.lottery_id'=> ['in', $lottery_id]])
                    ->field('c.expired_type, c.coupon_id, c.name, m.start_time, m.end_time, c.expired_date, c.type, c.description, c.address, c.supermarket, c.value, c.code_url, c.condition, c.create_time, c.uid, m.id as my_coupon_id')
                    ->order('m.id desc')
                    ->select();

                foreach ($coupon as &$vv)
                {
                    $create = date('Ymd', strtotime($vv['create_time']));
                    $vv['quan_id'] = $create.$vv['uid'].$vv['my_coupon_id'];
                }
            }

            $data['prize'] = [
                'is_prize'=> $coupon?1:0,
                'prize'=> $coupon?:[]
            ];

            $reward_data = db('lottery')->where(['award_id'=> $where['award_id']])->field('reward_id, level, customer_id, reward_name')->order('level')->select();
            $customer = db('customer')->where(['id'=> ['in', array_column($reward_data, 'customer_id')]])->field('id, nickname, avatar_url')->select();
            $customer2 = [];
            foreach ($customer as $v)
            {
                $customer2[$v['id']] = $v;
            }
            $m = [];
            foreach ($reward_data as $v)
            {
                $m[$v['level']]['name'] = $v['reward_name'];
                $m[$v['level']]['customer'][] = $customer2[$v['customer_id']];
            }

            $data['m'] = array_merge($m);

        }elseif ($data['status'] == 1) {
            $help = Help::alias('h')
                ->join('customer c', 'c.id = h.uid')
                ->where(['master_id'=> $customer_id])
                ->field('avatar_url, nickname')
                ->select();
            $data['help'] = $help;
            $data['count'] = Lottery::where($where)->count();
            $customer = Lottery::alias('l')
                ->join('customer c', 'c.id = l.customer_id')
                ->where(['l.award_id'=> $where['award_id']])
                ->order('l.lottery_id desc')
                ->limit(8)
                ->column('c.avatar_url');

            $data['customer'] = $customer;
        }

        $data['icon'] = Company::where(['id'=> $data['sponsor_id']])->value('cover');

        return $data;

    }

    function details2($where, $customer_id)
    {
        $data = db('award')->where($where)->find();
        $lottery_id = Lottery::where(['award_id'=> $where['award_id'], 'customer_id'=> $customer_id])->column('lottery_id');
        if($lottery_id)
        {
            $coupon = db('my_coupon')->alias('m')
                ->join('coupon c', 'c.coupon_id = m.coupon_id')
                ->where(['m.lottery_id'=> ['in', $lottery_id]])
                ->field('c.expired_type, c.coupon_id, c.name, m.start_time, m.end_time, c.expired_date, c.type, c.description, c.address, c.supermarket, c.value, c.code_url, c.condition, c.create_time, c.uid, m.id as my_coupon_id')
                ->order('m.id desc')
                ->select();

            foreach ($coupon as &$vv)
            {
                $create = date('Ymd', strtotime($vv['create_time']));
                $vv['quan_id'] = $create.$vv['uid'].$vv['my_coupon_id'];
            }
        }

        $data['prize'] = [
            'is_prize'=> $coupon?1:0,
            'prize'=> $coupon?:[]
        ];

        $reward_data = db('lottery')->where(['award_id'=> $where['award_id']])->field('reward_id, level, customer_id, reward_name')->order('level')->select();
        $customer = db('customer')->where(['id'=> ['in', array_column($reward_data, 'customer_id')]])->field('id, nickname, avatar_url')->select();
        $customer2 = [];
        foreach ($customer as $v)
        {
            $customer2[$v['id']] = $v;
        }
        $m = [];
        foreach ($reward_data as $v)
        {
            $m[$v['level']]['name'] = $v['reward_name'];
            $m[$v['level']]['customer'][] = [
                $customer2[$v['customer_id']]
            ];
        }

        $data['m'] = array_merge($m);


        return $data;

    }



    function more($where, $pageSize)
    {
        $customer = Lottery::alias('l')
            ->join('customer c', 'c.id = l.customer_id')
            ->where(['l.award_id'=> $where['award_id']])
            ->order('l.lottery_id desc')
            ->field('c.avatar_url')
            ->paginate($pageSize);

        return $customer;

    }

    function lottery($param)
    {
        $info = Award::where(['award_id'=> $param['award_id']])->field('status')->find();
        if($info['status'] == 0)
        {
            throw new CustomException("AWARD_NOT_FOUND");
        }

        if($info['status'] == 2)
        {
            throw new CustomException("AWARD_END");
        }

        Db::startTrans();
        try
        {
            $count = Lottery::where([
                'customer_id'=> $param['customer_id'],
                'award_id'=> $param['award_id']
            ])->count();

            if($count)
            {
                throw new CustomException("LOTTERY_ONE");
            }

            if($param['parent_id'])
            {
                Lottery::insert([
                    'customer_id'=> $param['customer_id'],
                    'award_id'=> $param['award_id'],
                    'num'=> 1,
                    'parent_id'=> $param['parent_id']
                ]);

                Help::insert([
                    'master_id'=> $param['parent_id'],
                    'uid'=> $param['customer_id']
                ]);

                Lottery::where(['award_id'=> $param['award_id'], 'customer_id'=> $param['parent_id']])->setInc('num');

            }else {
                Lottery::insert([
                    'customer_id'=> $param['customer_id'],
                    'award_id'=> $param['award_id'],
                    'num'=> 1,
                    'parent_id'=> 0
                ]);
            }

            Db::commit();

        }catch (Exception $exception){
            Db::rollback();
            throw new CustomException($exception->getMessage());
        }


    }

    function share($where)
    {
        $data = Award::where(['award_id'=> $where['award_id']])->field('name, cover')->find();
        $customer_id = Lottery::where(['award_id'=> $where['award_id'], 'is_prize'=> 1])->field('count(lottery_id) as num')->find();
        $customer = Customer::where(['id'=> $where['customer_id']])->field('nickname, avatar_url')->find();
        $count = Lottery::where(['award_id'=> $where['award_id']])->count('customer_id');
        $reward_id = Lottery::where(['award_id'=> $where['award_id'], 'customer_id'=> $where['customer_id']])->value('reward_id');

        $name = Reward::where(['reward_id'=> $reward_id])->value('name');

        return [
            'avatar_url'=> $customer['avatar_url'],
            'nickname'=> $customer['nickname'],
            'name'=> $name,
            'cover'=> $data['cover'],
            'total_num'=> $count,
            'probability'=> intval($count/$customer_id['num']*100).'%',
            'code'=> ''
        ];
    }

}